package com.one.mycommons;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateForm {
	
	private static final String date_format_second = "yyyy-MM-dd HH:mm:ss";
	
	private static final String date_format_second_non = "yyyy-MM-dd_HH-mm-ss";
	
	private static final String data_format_min = "yyyy-MM-dd HH:mm";
	
	private static final String data_format_day = "yyyy-MM-dd";
	
    private static ThreadLocal<DateFormat> threadLocal_second = new ThreadLocal<DateFormat>();
    
    private static ThreadLocal<DateFormat> threadLocal_second_non = new ThreadLocal<DateFormat>();
    
    private static ThreadLocal<DateFormat> threadLocal_min = new ThreadLocal<DateFormat>();
    
    private static ThreadLocal<DateFormat> threadLocal_day = new ThreadLocal<DateFormat>();
 
	public static String date2StringBysecond(Date date) {
		if (date == null) {
			return null;
		}
		DateFormat format = threadLocal_second.get();
		if(format == null){  
			format = new SimpleDateFormat(date_format_second);  
			threadLocal_second.set(format);  
        }
		return format.format(date);
	}
	
	public static String date2StringBysecondNon(Date date) {
		if (date == null) {
			return null;
		}
		DateFormat format = threadLocal_second_non.get();
		if(format == null){  
			format = new SimpleDateFormat(date_format_second_non);  
			threadLocal_second_non.set(format);  
        }
		return format.format(date);
	}
	
	public static String date2StringByMin(Date date) {
		if (date == null) {
			return null;
		}
		DateFormat format = threadLocal_min.get();
		if(format == null){  
			format = new SimpleDateFormat(data_format_min);  
			threadLocal_min.set(format);  
        }
		return format.format(date);
	}
	
	public static String date2StringByDay(Date date) {
		if (date == null) {
			return null;
		}
		DateFormat format = threadLocal_day.get();
		if(format == null){  
			format = new SimpleDateFormat(data_format_day);  
			threadLocal_day.set(format);  
        }
		return format.format(date);
	}
	
	public static Date string2DateBysecond(String date) {
		if (date == null) {
			return null;
		}
		date = date.trim();
		if (date.isEmpty()) {
			return null;
		}
		DateFormat format = threadLocal_second.get();
		try {
			if(format == null){  
				format = new SimpleDateFormat(date_format_second);
				threadLocal_second.set(format);  
			}
			return format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date string2DateByMin(String date) {
		if (date == null) {
			return null;
		}
		date = date.trim();
		if (date.isEmpty()) {
			return null;
		}
		DateFormat format = threadLocal_min.get();
		try {
			if(format == null){  
				format = new SimpleDateFormat(data_format_min);
				threadLocal_min.set(format);  
			}
			return format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date string2DateByDay(String date) {
		if (date == null) {
			return null;
		}
		date = date.trim();
		if (date.isEmpty()) {
			return null;
		}
		DateFormat format = threadLocal_day.get();
		try {
			if(format == null){  
				format = new SimpleDateFormat(data_format_day);
				threadLocal_day.set(format);  
			}
			return format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
